package com.fw.ui.view.profile.service.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.vaadin.dialogs.ConfirmDialog;

import com.fw.ui.ApplicationUI;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.common.ConfirmDialogWindow;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.fw.ui.view.profile.SearchServiceProfileView;
import com.vaadin.navigator.Navigator;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.VerticalLayout;

@Component
@Scope(SCOPE_PROTOTYPE)
public class SearchServiceProfileNewButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;
    @Autowired
    private PropertyBAO propertyBAO;
    @Autowired
    private MessageBAO messageBAO;

    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	final SearchServiceProfileView searchServiceProfileView = (SearchServiceProfileView) this.getBasedComponent();

	String title = messageBAO.getMessage("com.fw.ui.view.profile.SearchServiceProfileNew.message.confirm.title");
	String okCaption = messageBAO.getMessage("application.button.ok.caption");
	String cancelCaption = messageBAO.getMessage("application.button.cancel.caption");

	String optionTitle = messageBAO.getMessage("com.fw.ui.view.profile.SearchServiceProfileNew.message.optionTitle");
	final String option1 = messageBAO.getMessage("com.fw.ui.view.profile.SearchServiceProfileNew.message.option1");
	final String option2 = messageBAO.getMessage("com.fw.ui.view.profile.SearchServiceProfileNew.message.option2");
	final String option3 = messageBAO.getMessage("com.fw.ui.view.profile.SearchServiceProfileNew.message.option3");
	final VerticalLayout verticalLayout = new VerticalLayout();
	final OptionGroup optionGroup = new OptionGroup(optionTitle);
	optionGroup.addItem(option1);
	optionGroup.addItem(option2);
	optionGroup.addItem(option3);
	// Disable one item
	optionGroup.setItemEnabled(option2, false);
	optionGroup.setItemEnabled(option3, false);
	optionGroup.setNullSelectionAllowed(false);
	optionGroup.setValue(option1);

	verticalLayout.setMargin(true);
	verticalLayout.setSpacing(true);
	verticalLayout.addComponent(optionGroup);
	verticalLayout.setComponentAlignment(optionGroup, Alignment.TOP_CENTER);

	ConfirmDialogWindow.createConfirmDialog(searchServiceProfileView.getUI(), title, verticalLayout, okCaption, cancelCaption,
		new ConfirmDialog.Listener() {
		    private static final long serialVersionUID = 1L;

		    @Override
		    public void onClose(ConfirmDialog dialog) {
			if (dialog.isConfirmed()) {
			    if (option1.equals(optionGroup.getValue())) {
				Navigator navigator = ((ApplicationUI) searchServiceProfileView.getUI()).getNavigator();
				String landing = propertyBAO.getPropertyValue("view.profile-service-cash-create.path");
				navigator.navigateTo(landing);
			    }
			}
		    }
		}, "240px", "300px");

	return null;

	// Navigator navigator = ((ApplicationUI) searchServiceProfileView.getUI()).getNavigator();
	// String landing = propertyBAO.getPropertyValue("view.profile-merchant-create.path");
	// navigator.navigateTo(landing);
	// return null;
    }

    @Override
    public boolean validate(ClickEvent event) {

	return true;
    }

}
